﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForPayReqBill : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //请款单
            UFIDA.U9.AP.Payment.PayReqFundHead payReq = key.GetEntity() as UFIDA.U9.AP.Payment.PayReqFundHead;
            if (payReq == null) return;

            #region 请款单(请款用途为标准或预付款)需控制请款单申请日期不能小于来源单据(应付单,采购订单,收货单)日期
            //判断请款单是否为开立状态
            if (payReq.DocStatus== InDirectHandleBillDocStatusEnum.Open)
            {
                foreach (var line in payReq.PayReqFundUses)
                {
                    foreach (var subline in line.PayReqFundDetails)
                    {
                        //判断请款用途是否为标准
                        if (line.ReqFundUse == RequestFundUseEnum.Standard)
                        {
                            //请款明细来源单据类型等于应付单
                            if (subline.SrcBillType == PayReqFundSrcDocTypeEnum.APBill)
                            {
                                UFIDA.U9.AP.APBill.APBillHead apdoc = UFIDA.U9.AP.APBill.APBillHead.Finder.FindByID(subline.SrcBillID);
                                if (apdoc == null)
                                {
                                    throw new Exception($"查找应付单失败!应付单[{subline.SrcBillNum}]");
                                }
                                //判断应收单日期与请款单申请日期
                                if (apdoc.BusinessDate > payReq.PayRFDate)
                                {
                                    throw new Exception("申请日期应大于来源单据日期");
                                }
                            }
                        }
                        //判断请款用途是否为预付款
                        else if (line.ReqFundUse == RequestFundUseEnum.PrePay)
                        {
                            //请款明细来源单据类型等于采购订单
                            if (subline.SrcBillType == PayReqFundSrcDocTypeEnum.PO)
                            {
                                UFIDA.U9.PM.PO.PurchaseOrder order = UFIDA.U9.PM.PO.PurchaseOrder.Finder.FindByID(subline.SrcBillID);
                                if (order == null)
                                {
                                    throw new Exception($"查找采购订单失败!采购订单[{subline.SrcBillNum}]");
                                }
                                //判断应付单日期与请款单申请日期
                                if (order.BusinessDate > payReq.PayRFDate)
                                {
                                    throw new Exception("申请日期应大于来源单据日期");
                                }
                            }
                            //请款明细来源单据类型等于收货单
                            else if (subline.SrcBillType == PayReqFundSrcDocTypeEnum.RCV)
                            {                             
                                UFIDA.U9.PM.Rcv.Receivement rcv = UFIDA.U9.PM.Rcv.Receivement.Finder.FindByID(subline.SrcBillID);
                                if (rcv == null)
                                {
                                    throw new Exception($"查找收货单失败!收货单[{subline.SrcBillNum}]");
                                }
                                //判断收货单日期与请款单申请日期
                                if (rcv.BusinessDate > payReq.PayRFDate)
                                {
                                    throw new Exception("申请日期应大于来源单据日期");
                                }
                                
                            }
                        }
                    }
                }
            
            
            }
            #endregion

        }

    }
}
